<template>
  <div class="homeLogin">
    <el-row :span="24" class="login-head">
      <div class="registerRow">
        <p>登 陆</p>
        <p>欢迎来到甘乐源销售管理平台</p>
        <!-- <span @click="routerGo()">注册</span> -->
      </div>
    </el-row>
    <el-row :span="24" class="login-main">
      <el-form :rules="rules" :ref="formRefs" :model="user"  class="login-form">
         <el-form-item prop="userID">
          <el-input placeholder="用户名" v-model="user.userID"></el-input>
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input placeholder="密码" type="password" v-model="user.passWord"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input  placeholder="输入验证码" v-model="user.yzm"></el-input>
        </el-form-item>
        <div class="yzm">
          <input type="text" id="checkCode" class="code" style="width: 108px" v-model="user.code"/>
          <a href="#" @click="createCode" class='change'>看不清楚</a> 
        </div>
         <el-form-item>
           <el-button class="loginbtn" @click.prevent="userLogin()">登 陆</el-button>
         </el-form-item>
      </el-form>
    </el-row>
    <el-row :span="24" class="forget">
      <!-- <span @click="rem">sdafsdfsadfas</span> -->
     <el-checkbox v-model="remember" style="float:left;margin-left:10%;color:#499E41;">记住密码</el-checkbox> <span style="float:right" @click="forgetps">忘记密码？</span>
    </el-row>
  </div>
</template>

<script>
import {saveToSession} from '../common/sessionStorage.js'
export default {
  beforeCreate () {
    this.formRefs = 'loginForm'
  },
  components: {
    saveToSession
  },
  created () {
    if (localStorage.getItem('userID')) {
      this.user.userID = localStorage.getItem('userID')
      this.user.passWord = localStorage.getItem('passWord')
    }
    this.createCode()
  },
  data () {
    return {
      projectBaseUrl: 'http://218.104.153.143:8089',
      rules: {
        userID: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { message: '', trigger: 'change' }
        ],
        passWord: [
            { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      user: {
        userID: '',
        passWord: '',
        code: '',
        yzm: ''
      },
      pageTitle: { // 页面标题
        home: '首页',
        shopping: '进货商城',
        productDetails: '商品详情',
        uploadOrder: '提交订单',
        confirmmOrder: '确认订单',
        withDraw: '提现',
        withDrawRecord: '提现记录',
        submitSuccess: '提交成功',
        myBankcard: '我的银行卡',
        addBankcard: '添加银行卡',
        myAddress: '我的收货地址',
        newAddress: '添加新地址',
        myOrder: '我的订单',
        areaOrder: '区域业绩',
        disOrder: ' 分销订单',
        commission: '历史收益明细',
        distributor: '我的分享',
        code: '我的邀请二维码',
        changeps: '修改密码'
      },
      remember: false,
      code: '',
      arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    }
  },
  methods: {
    routerGo () { // 路由跳转
      this.$router.push('/register')
    },
    userLogin () {
      let that = this
      if (!that.user.yzm) {
        that.$message({
          message: '验证码不能为空',
          type: 'error',
          duration: 1000,
          showClose: false
        })
      } else if (that.user.yzm.toUpperCase() !== that.user.code.toUpperCase()) {
        that.$message({
          message: '验证码不正确',
          type: 'error',
          duration: 1000,
          showClose: false
        })
        that.user.yzm = ''
        that.createCode()
      } else {
        this.$refs[this.formRefs].validate((valid) => {
          if (valid) {
            that.$http.post(this.projectBaseUrl + '/numberone-auth/busiUserLogin/login.shtml', that.user, {
              emulateJSON: true
            }).then((data) => {
              if (data.body.success === '登录成功') {
                saveToSession('setUser', data.body.user)
                saveToSession('setToken', data.body.token)
                saveToSession('setTitle', this.pageTitle)
                that.$store.dispatch('setUser', data.body.user)
                that.$store.dispatch('setToken', data.body.token)
                that.$store.dispatch('setTitle', this.pageTitle)
                if (that.remember) {
                  console.log(11111)
                  localStorage.setItem('userID', that.user.userID)
                  localStorage.setItem('passWord', that.user.passWord)
                }
                that.$router.push('/home')
              } else {
                that.$message({
                  message: data.body.error,
                  type: 'error',
                  duration: 1000,
                  showClose: false
                })
              }
            }, (ne) => {
              that.$message({
                message: '网络错误',
                type: 'error',
                duration: 1000,
                showClose: false
              })
            })
          } else {
            return false
          }
        })
      }
    },
    forgetps () {
      this.$router.push('/forgetPs')
    },
    createCode () {
      this.user.code = ''
      this.code = ''
      let codeLength = 4
      for (var i = 0; i < codeLength; i++) {
        var charIndex = Math.floor(Math.random() * 60)
        this.code += this.arr[charIndex]
      }
      if (this.code.length !== codeLength) {
        this.createCode()
      }
      this.user.code = this.code
    }
  }
}
</script>

<style lang="scss" type="stylesheet/scss">
  .code{ 
    float: left;
    // background-image : url(qqq.png); 
    font-family:Arial;
    font-style:italic;
    font-size: 14px;
    color:Red;
    border:0;
    padding:2px 3px;
    letter-spacing:3px;
    font-weight:bolder;
     background: url('../../assets/imgs/yzm.png') center center no-repeat;
    background-size: cover; 
    }
  .homeLogin{
    font-size: 12px;
    .el-checkbox__input.is-checked .el-checkbox__inner{
      background-color:#499E41;
    }
    .el-checkbox__inner:hover{
      border-color:#499E41;
    }
    .login-head{
      color:#fff;
      div{
        height: 201px;
        background: url('../../assets/imgs/bg.png') center center no-repeat;
        background-size: cover;
        p{
          font-size: 14px;
        }
        p:first-of-type{
          font:700 16px/80px "微软雅黑";
          margin-bottom: 15px;
        }
        span{
          float: right;
          font-size: 14px;
          font-weight: 700;
          position: absolute;
          right: 10px;
          top: 30px;
          display: inline-block;
          width: 50px;
          line-height: 20px;
        }
        .registerRow {
          display: flex;
          .register {
            display: inline-block;
            width: 50px;
            font-weight: 700;
            font-size: 14px;
            position: absolute;
            right: 10px;
          }
        }
      }
    }
    .login-main{
      margin-top: 10px;
      .el-form-item__error{
        left: 10%
      }
      input{
        width: 90%;
        height: 44px;
        border-radius: 20px;
        text-indent: 34px;
      }
      .icon-num{
            font-size: 20px;
            color: #bfcbd9;
            position: absolute;
            top: 12px;
            left: 36px;
      }
      .icon-code{
          font-size: 29px;
          color: #bfcbd9;
          position: absolute;
          top: 7px;
          left: 39px;
      }
      .codebtn{
        color:#fff;
        width: 25%;
        height: 44px;
        background-color: #499E41;
        border-radius: 0 20px 20px 0;
        margin-left: -20px;
        border: none;
      }
      .code-input{
        width: 75%;
        float: left;
        margin-left: 5px;
      }
      .code-input input{
        width: 100%;
      }
      .loginbtn{
        width: 90%;
        height: 44px;
        background-color: #499E41;
        margin-top: 30px;
        border-radius: 20px;
        color:#fff;
        font-size: 16px;
      }
    }
    .forget{
      color:#499E41;
      margin-right:10%;
      font-size: 14px;
      margin-top: 20px;
      .rem{
        width: 14px;
        height: 14px;
        float: left;
        color: #499E41;
        border:1px solid #499E41;
        margin-left: 10%;
      }
      span{
        // float: right;
      }
    }
    .yzm{
      height: 44px;
      width:90%;
      margin:0 auto !important;
    }
    .change {
     line-height:44px;
     float: left;
     margin-left:10px; 
    }
  }
</style>
